using System;
using GameStateManagement.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;

namespace GameStateManagement.View.Gui.Components
{
    public abstract class GuiComponent : ICanHandleInput
    {
        #region Fields

        protected int Width;
        protected int Height;
        protected bool HasBeenClicked;
        protected Rectangle Rectangle;
        protected Vector2 Position;
        protected Color Color;
        #endregion

        #region Events

        public delegate void ClickedEventHandler(object sender, EventArgs e);
        public event ClickedEventHandler Clicked;

        #endregion

        protected GuiComponent(Vector2 position, int width, int height)
        {
            Position = position;
            Width = width;
            Height = height;
            Rectangle = new Rectangle((int)position.X, (int)position.Y, Width, Height);
            Color = Color.White;
        }
        
        public bool IsClicked(Vector2 clickPosition)
        {
            if (Rectangle.Contains((int)clickPosition.X, (int)clickPosition.Y))
            {
                if (Clicked != null)
                {
                    Clicked(this, new EventArgs());
                }
                HasBeenClicked = true;
                return true;
            }

            return false;
        }

        public void Move(Vector2 newPosition)
        {
            Position.X = (int)newPosition.X;
            Position.Y = (int)newPosition.Y;
        }

        public abstract void Update(GameTime gameTime);

        public abstract void Draw(GameTime gameTime, SpriteBatch spriteBatch);

        public bool HandleInput(InputState state)
        {
            return false;
        }
    }
}
